export default function combineReducers(reducers) {
    // 其实还是返回一个reducer,接收state和action
    return function combination(state = {}, action) {
        let nextState = {};
        let hasChanged = false;
        for (let key in reducers) {
            let reducer = reducers[key]
            nextState[key] = reducer(state[key], action);
            // 判断是否发生改变,否则用上一次的结果就可以了
            hasChanged = hasChanged || nextState[key] !== state[key];
        }

        // {a:1,b:2} {a:1} replaceReducer可能删减了一些reducer导致数量不相等
        hasChanged = hasChanged || Object.keys(nextState).length !== Object.keys(state).length
        return hasChanged ? nextState : state;
    }
}

